<?php

namespace app\beautiful\model\mall;

use app\common\model\CommonModel;
use think\facade\Db;


class FlashSaleGoodsSkuModel extends CommonModel{

    protected  $name = 'beautiful_flash_sale_goods_sku';

    protected  $pk   = 'flash_sale_goods_sku_id';


    public function goods(){

        return  $this->hasOne(GoodsModel::class,'goods_id','goods_id');

    }
    public function sku(){

        return  $this->hasOne(GoodsSkuModel::class,'sku_id','sku_id');

    }
    public static function list($where,$order,$limit=0,$page=0){
        $query = Db::name('beautiful_flash_sale_goods_sku')->alias('fs')
            ->leftjoin('beautiful_goods_sku s','s.sku_id=fs.sku_id')
            ->leftjoin('beautiful_goods_other_sku_detail sd','sd.other_sku_detail_id=s.unit_id')
            ->where(['fs.is_delete'=>0])
            ->when(isset($where['shop_id']),function ($query) use ($where){
                $query->where(['fs.shop_id'=>$where['shop_id']]);
            })
            ->when(isset($where['flash_sale_goods_id']),function ($query) use ($where){
                $query->where(['fs.flash_sale_goods_id'=>$where['flash_sale_goods_id']]);
            })
            ->field("fs.start_sales_num,fs.flash_sale_goods_sku_id,fs.name,fs.original_price,fs.flash_sale_price,fs.quota,sd.group_detail_name as unit");
        $count=$query->count();
        $query=$query->order($order);
        if($limit>0) $query=limit($limit)->page($page);
        $list=$query->select()->toArray();
        if(!empty($list)){

        }
        return compact('count','list');
    }
}

